package com.xyz.readWriteLock;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author 肖咉卓
 *
 * 阻塞以列
 * 1 两个数据结构:栈/队列
 *     1.1栈     后进先出
 *     1.2队列   先进先出
 *
 * 2 用寡以列
 *    2.1阻塞  必须要阻寤/不得不阻寤
 *
 * 3 how
 */
public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        /*
        //抛出异常组
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        //System.out.println(blockingQueue.add("d"));
        //查看首个元素
        System.out.println(blockingQueue.element()+"\t查询第一个");
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());*/

        /*//特使值返回true 或者 false
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d"));
        System.out.println(blockingQueue.peek()+"\t查询第一个");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());*/


        /*//阻塞，当满了后进入阻塞状态，等有空位在进去
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        //blockingQueue.put("d");
        System.out.println(blockingQueue.take()+"\t拿出");
        System.out.println(blockingQueue.take()+"\t拿出");
        System.out.println(blockingQueue.take()+"\t拿出");*/

        /*//设置阻塞后等待的时间
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("a",3L,TimeUnit.SECONDS));*/

    }
}
